﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace RSALicCore
{
    /// <summary>
    /// 私钥授权管理器
    /// </summary>
    public class PriLicManager
    {
        protected RSACryptoServiceProvider rsa;

        /// <summary>
        /// 构造一个管理器
        /// </summary>
        /// <param name="prikey"></param>
        public PriLicManager(string prikey)
        {
            rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(prikey);
        }

        /// <summary>
        /// 生成一个授权
        /// </summary>
        /// <param name="licData"></param>
        /// <returns></returns>
        public virtual string MakeLic(string licData)
        {
            var data = Convert.FromBase64String(licData);
            var decBytes = rsa.Decrypt(data, fOAEP);

            RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(rsa);
            formatter.SetHashAlgorithm(HashAlgorithm);
            byte[] inArray = formatter.CreateSignature(decBytes);
            return Convert.ToBase64String(inArray);
        }

        protected static bool fOAEP = false;

        protected static string HashAlgorithm => HardwareInfoReader.HashAlgo;
    }
}
